#include "KIMI_UI.h"

// WiFi输入框
static lv_obj_t *wifi_ssid_input;
static lv_obj_t *wifi_password_input;

// 连接WiFi按钮
static lv_obj_t *connect_button;

// 键盘对象
static lv_obj_t *keyboard;

// 日志标签
static const char *TAG = "WiFi Connect UI";

// 连接WiFi的回调函数
void connect_wifi(lv_event_t *e) {
    const char *ssid = lv_textarea_get_text(wifi_ssid_input);
    const char *password = lv_textarea_get_text(wifi_password_input);

    if (ssid == NULL || password == NULL || ssid[0] == '\0' || password[0] == '\0') {
        ESP_LOGE(TAG, "SSID or password is invalid");
        return;
    }

    ESP_LOGI(TAG, "Connecting to WiFi...");
    ESP_LOGI(TAG, "SSID: %s", ssid);
    ESP_LOGI(TAG, "Password: %s", password);
    wifi_init_sta(ssid, password);
}

// 键盘事件回调
void keyboard_event_cb(lv_event_t *e) {
    lv_obj_t *kb = e->current_target; // 获取键盘对象

    // 获取键盘绑定的输入框
    lv_obj_t *textarea = lv_keyboard_get_textarea(kb);

    // 检查 textarea 是否有效
    if (textarea == NULL) {
        ESP_LOGE(TAG, "Textarea is NULL");
        return;
    }

    // 获取当前激活的输入设备
    lv_indev_t *indev = lv_indev_get_act();
    if (indev == NULL) {
        ESP_LOGE(TAG, "Input device is NULL");
        return;
    }

    // 获取当前焦点所在的对象
    lv_group_t *group = indev->group;
    if (group == NULL) {
        ESP_LOGE(TAG, "Group is NULL");
        return;
    }

    lv_obj_t *active_ta = lv_group_get_focused(group);
    if (active_ta == NULL) {
        ESP_LOGE(TAG, "Active textarea is NULL");
        return;
    }

    // 更新当前激活的输入框的内容
    const char *txt = lv_textarea_get_text(textarea);
    lv_textarea_set_text(active_ta, txt);
}

// 输入框焦点事件回调
static void textarea_focus_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);

    if(code == LV_EVENT_FOCUSED) {
        // 输入框获得焦点，显示键盘
        lv_keyboard_set_textarea(keyboard, ta);
        lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
    } else if(code == LV_EVENT_DEFOCUSED) {
        // 输入框失去焦点，隐藏键盘
        lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}

// 创建WiFi输入界面
void create_wifi_connect_ui(lv_obj_t *parent) {
    // 创建一个容器
    lv_obj_t *container = lv_obj_create(parent);
    lv_obj_set_size(container, lv_pct(100), lv_pct(100));
    lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建WiFi SSID输入框
    wifi_ssid_input = lv_textarea_create(container);
    lv_textarea_set_one_line(wifi_ssid_input, true);
    lv_obj_set_width(wifi_ssid_input, lv_pct(80));
    lv_textarea_set_placeholder_text(wifi_ssid_input, "Enter WiFi SSID");
    // 添加焦点事件回调
    lv_obj_add_event_cb(wifi_ssid_input, textarea_focus_event_cb, LV_EVENT_ALL, NULL);

    // 创建WiFi密码输入框
    wifi_password_input = lv_textarea_create(container);
    lv_textarea_set_one_line(wifi_password_input, true);
    lv_obj_set_width(wifi_password_input, lv_pct(80));
    lv_textarea_set_placeholder_text(wifi_password_input, "Enter WiFi Password");
    lv_textarea_set_password_mode(wifi_password_input, true);
    // 添加焦点事件回调
    lv_obj_add_event_cb(wifi_password_input, textarea_focus_event_cb, LV_EVENT_ALL, NULL);

    // 创建连接WiFi按钮
    connect_button = lv_btn_create(container);
    lv_obj_set_width(connect_button, lv_pct(80));
    lv_obj_add_event_cb(connect_button, connect_wifi, LV_EVENT_CLICKED, NULL);
    lv_obj_t *label = lv_label_create(connect_button);
    lv_label_set_text(label, "Connect to WiFi");

    // 创建键盘
    keyboard = lv_keyboard_create(container);
    lv_obj_set_width(keyboard, lv_pct(100));
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN); // 默认隐藏键盘

    // 创建焦点分组
    lv_group_t *g = lv_group_create();
    lv_group_add_obj(g, wifi_ssid_input);
    lv_group_add_obj(g, wifi_password_input);
    lv_group_add_obj(g, connect_button);

    // 设置默认焦点为SSID输入框
    lv_group_focus_obj(wifi_ssid_input);

    // 将键盘绑定到SSID输入框
    lv_keyboard_set_textarea(keyboard, wifi_ssid_input);

    // 添加键盘事件回调
    lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    // 设置输入设备的焦点分组
    lv_indev_t *indev = lv_indev_get_next(NULL);
    if (indev) {
        lv_indev_set_group(indev, g);
    } else {
        ESP_LOGE(TAG, "No input device found");
    }
}

// 初始化UI
void I_init_wifi_connect_ui(void) {
    static bool lvgl_initialized = false;
    if (!lvgl_initialized) {
        lv_init();
        lvgl_initialized = true;
    }

    // 创建屏幕
    lv_obj_t *scr = lv_scr_act();
    create_wifi_connect_ui(scr);
}